package ldh.im.fxgui.controller;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Separator;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.text.TextFlow;
import ldh.im.fxbase.pojo.ImGroup;
import ldh.im.fxbase.util.DateUtil;
import ldh.im.fxgui.util.UiUtil;
import org.kordamp.ikonli.javafx.FontIcon;

import java.net.URL;
import java.util.ResourceBundle;

public class MItemController implements Initializable {

    private ImGroup mItem;

    @FXML private Pane root;
    @FXML private ImageView headImageView;
    @FXML private Label nameLabel;
    @FXML private TextFlow lastedMessageTextFlow;
    @FXML private Label createDateLabel;
    @FXML private Label tipLabel;

    @FXML private FontIcon closeIcon;

    private MessageController messageController;
    private Pane popupPane = null;

    public void setData(ImGroup mItem) {
        this.mItem = mItem;
        nameLabel.setText(mItem.getName());
        createDateLabel.setText(DateUtil.format(mItem.getCreateTime(), "YY/MM/dd"));
        lastedMessageTextFlow.getChildren().add(new Label(mItem.getNewMessage()));

        Image image = new Image(this.getClass().getResource("/imgs/head.jpg").toExternalForm());
        headImageView.setImage(image);
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        root.setOnMouseEntered(e->{
            closeIcon.setVisible(true);
        });
        root.setOnMouseExited(e->{
            closeIcon.setVisible(false);
        });

        root.setOnMousePressed(e->{
            if (e.getButton() != MouseButton.SECONDARY) return;
            if (popupPane == null) {
                popupPane = buildPopPane();
                UiUtil.getMainPopup().getContent().clear();
                UiUtil.getMainPopup().getContent().add(popupPane);
            }
            UiUtil.getMainPopup().show(root.getScene().getWindow(), e.getScreenX(), e.getScreenY());
        });
    }

    public void removeItem(MouseEvent mouseEvent) {
        if (messageController == null) return;
        messageController.remove(mItem);
    }

    public void setMessageController(MessageController messageController) {
        this.messageController = messageController;
    }

    private Pane buildPopPane() {
        VBox vBox = new VBox();
        vBox.getStyleClass().add("mitem-pop-pane");
        Button b1 = new Button("置顶");
        if (mItem.isTop()) {
            b1.setText("取消置顶");
        }
        b1.setOnAction(e->{
            mItem.setTop(!mItem.isTop());
            messageController.reloadMItem(mItem, true);
            UiUtil.getMainPopup().hide();
        });
        Button b2 = new Button("消息免打扰");
        Button b3 = new Button("修改群名称");
        Button b4 = new Button("在独立窗口打开");
        Separator separator = new Separator();
        Button b5 = new Button("删除聊天");
        b5.setOnAction(e->removeItem(null));
        vBox.getChildren().addAll(b1, b2, b3, b4, separator, b5);
        return vBox;
    }
}
